package com.yazi.mxz.mobile.controller.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Sort;
import com.myframework.smhj.jackson.result.PageResult;
import com.yazi.mxz.common.entity.beans.MemberSpend;
import com.yazi.mxz.common.service.MemberSpendService;
import com.yazi.mxz.mobile.jackson.filter.MemberSpendFilter;

@Controller("user-memberSpendController")
@RequestMapping("/user/memberSpend")
public class MemberSpendController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(MemberSpendController.class);

	@Resource
	MemberSpendService memberSpendService;

	@RequestMapping("/list")
	public String list() {
		log.debug("进入消费记录页面");
		return "v1.5/user/memberSpend_list";
	}

	@RequestMapping("/list/json")
	@ResponseBody
	public PageResult<?> listJson(MemberSpend memberSpend, Searching si) {
		log.debug("获取消费记录列表");
		PageResult<MemberSpend> jsonResult = new PageResult<MemberSpend>();
		jsonResult.setFilter(MemberSpendFilter.get4List());
		try {
			if (null == si.getSorts()) {
				List<Sort> sorts = new ArrayList<Sort>();
				sorts.add(new Sort("spendTime", true));
				si.setSorts(sorts);
			}
			Page<MemberSpend> page = memberSpendService.list(memberSpend, si);
			jsonResult.setResult(page);
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
			log.error("获取消费记录列表异常:", e);
		}

		return jsonResult;
	}

	@RequestMapping("/addDo")
	public String addDo(String serialCode, Model model) {
		log.debug("添加消费记录");
		try {
			memberSpendService.addByMember(getMember(), serialCode);
		} catch (MyException e) {
			model.addAttribute(TIP_MSG, "消费记录添加失败：" + e.getMessage());
			return FORWARD_TIP;
		} catch (Exception e) {
			model.addAttribute(TIP_MSG, "消费记录添加失败：" + e.getMessage());
			log.error("消费记录添加异常:", e);
			return FORWARD_TIP;
		}
		return "user/memberSpend_addDo_success";
	}

}
